package com.ushio.gougouwangguo.controller;

import com.ushio.commonmodule.pojo.result.Result;
import com.ushio.gougouwangguo.entity.mysql.UserTasks;
import com.ushio.gougouwangguo.entity.qo.UserTasksQo;
import com.ushio.gougouwangguo.entity.vo.UserTasksVo;
import com.ushio.gougouwangguo.service.IUserTasksService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户任务 前端控制器
 * </p>
 *
 * @author 张文凯
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/userTasks")
@Tag(name="用户任务",description = "心跳")
public class UserTasksController {

    @Autowired
    IUserTasksService iUserTasksService;
    @PostMapping("save-the-heartbeat")
    @Operation(summary = "心跳补齐接口",description = "心跳,如果用户离线,在执行的任务,不停止,等下次登录 重新启动该任务,并补齐获得的物品")
    public Result<List<UserTasksVo>> saveTheHeartbeat(@RequestBody UserTasksQo userTasksQo){


        try{

            List<UserTasksVo> userTasksVoList = iUserTasksService.saveTheHeartbeat(userTasksQo);

            return Result.success(userTasksVoList);

        }catch (Exception e){

            e.printStackTrace();

            return Result.fail(e.getMessage());
        }

    };
    @PostMapping("/save-user-tasks")
    @Operation(summary = "保存心跳",description = "用户开始任务时如果有该任务记录修改,没有改任务记录删除")
    public Result<UserTasksVo> saveUserTasks(UserTasksQo userTasksQo){


        try{

            UserTasksVo userTasksVo = iUserTasksService.saveUserTasks(userTasksQo);

            return Result.success(userTasksVo);

        }catch (Exception e){

            e.printStackTrace();

            return Result.fail(e.getMessage());
        }

    };

    @PostMapping("/stop-the-heartbeat")
    @Operation(summary = "停止心跳",description = "停止任务时,任务状态修改,防止下次会自动补齐")
    public Result<UserTasksVo> stopTheHeartbeat(@RequestBody UserTasksQo userTasksQo){


        try{

            UserTasksVo userTasksVo = iUserTasksService.stopTheHeartbeat(userTasksQo);

            return Result.success(userTasksVo);

        }catch (Exception e){

            e.printStackTrace();

            return Result.fail(e.getMessage());
        }

    };

}
